import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  base: '/',
  server: {
    proxy: {
      '/api': {
        target: 'https://static-test.bbxhc.com/',
        changeOrigin: true,
        rewrite: path => {
          const newPath = path.replace(/^\/api/, '')
          console.log(`Proxy rewrite: ${path} -> ${newPath}`)
          return newPath
        },
        secure: false,
        headers: {
          'user-token': '12306070809_fa7ee89eeebceefce79131b20a9a3e00',
        },
        configure: proxy => {
          proxy.on('proxyReq', proxyReq => {
            console.log('Proxy Request to:', proxyReq.path)
          })
        },
      },
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
})
